package com.itheima.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.exception.BusinessException;
import com.itheima.manager.MyAdminManager;
import com.itheima.service.db.AdminService;
import com.itheima.util.JwtUtil;;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MyAdminController {
    @Autowired
    private MyAdminManager myAdminManager;
    @Reference
    private AdminService adminService;
    /**
     * 生成图形验证码
     *
     * @param response 响应对象
     * @throws IOException
     */
    @GetMapping("/admin/verification")
    public void sendCode(HttpServletResponse response) throws IOException {
        //生成验证码
        LineCaptcha lineCaptcha = myAdminManager.sendCode();
        //返回浏览器
        lineCaptcha.write(response.getOutputStream());
    }

    /**
     * 登录
     *
     * @param map 前端传递的参数集合
     * @return
     */
    @PostMapping("/admin/login")
    public Map login(@RequestBody Map<String, String> map) throws BusinessException {
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String token = myAdminManager.login(username, password, verificationCode);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        return tokenMap;
    }

    //通过微信code登录
    @RequestMapping(value="/weixinlogin")
    public void wxLogin(String code, HttpServletResponse response, HttpServletRequest request) throws IOException {
        Admin admin = adminService.wxLogin(code);
        if (code == null) {
            response.sendRedirect("http://note.java.itcast.cn/login.html");
        }
        Map<String,String> map=new HashMap<>();
        map.put("id", admin.getId().toString());
        map.put("username", admin.getUsername());
        String token = JwtUtil.createToken(map);
        admin.setToken(token);

        response.sendRedirect("http://note.java.itcast.cn/main.html");
    }

    /*@GetMapping(value = "/weixinlogin")
    public String wxLogin(String code, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("微信登录");
        Admin admin = adminService.wxLogin(code);
        //ModelAndView modelAndView = new ModelAndView();
        if (admin == null) {
            //modelAndView.setViewName("redirect:/login.html");
            //modelAndView.addObject("admin", admin);
            return "redirect:http://note.java.itcast.cn/login.html";
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("id", admin.getId().toString());
        map.put("username", admin.getUsername());
        //生成token
        String token = JwtUtil.createToken(map);
        admin.setToken(token);
        //modelAndView.setViewName("forward:/main.html");
        //modelAndView.addObject("admin", admin);
        //System.out.println(modelAndView);
        request.setAttribute("admin", admin);

        //request.getRequestDispatcher("http://note.java.itcast.cn/main.html").forward(request, response);
        return "redirect:http://note.java.itcast.cn/main.html";
    }*/
}
